package com.noah.project.controller

import com.noah.project.global.Const
import com.noah.project.global.JsonManager
import com.noah.project.model.Account
import com.noah.project.model.Friend
import com.noah.project.model.User
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import java.util.*


/**
 * Created by Wiki on 2018/5/25.
 */

@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UserApiControllerTest {
    @Autowired
    lateinit var controller: UserApiController
    val decoder = Base64.getDecoder()
    val encoder = Base64.getEncoder()

    @Test
    @Throws(Exception::class)
    fun register() {
        val account = Account(username = "test2", password = "E10ADC3949BA59ABBE56E057F20F883E")
        val reply = controller.register(account, "测试2")
        when (reply.code) {
            Const.SUCCESS -> println("注册成功 " + reply.toString())
            Const.FAIL -> println("注册失败")
            Const.ERROR_USERNAME -> println("账号已注册")
        }
    }

    @Test
    @Throws(Exception::class)
    fun login() {
        val user = Account(username = "test2", password = "E10ADC3949BA59ABBE56E057F20F883E")
        val reply = controller.login(user, null)
        when (reply.code) {
            Const.SUCCESS -> {
                println("登录成功 " + reply.toString())
                val retUser = JsonManager.fromJson(reply.data, User::class.java)
                println(String(decoder.decode(retUser?.token?.toByteArray())))
                println(String(decoder.decode("MTAxMjAwMzoyMDA6MjAxODA4MTYxMDA5MTgwNDQ=".toByteArray())))
            }
            Const.ERROR_USERNAME -> println("账号未注册")
            Const.ERROR_PASSWORD -> println("密码错误")
        }
    }

    @Test
    @Throws(Exception::class)
    fun loginToken() {
        val reply = controller.login(null, "MjoxNTcwMjQxNTc1OTY0")
        when (reply.code) {
            Const.SUCCESS -> {
                println("登录成功 " + reply.toString())
                val user = JsonManager.fromJson(reply.data, User::class.java)
                println(String(decoder.decode(user?.token?.toByteArray())))
            }
            Const.ERROR_USERNAME -> println("账号未注册")
            Const.ERROR_PASSWORD -> println("密码错误")
        }
    }

    @Test
    @Throws(Exception::class)
    fun update() {
        val user = User(uid = 2, name = "王小二", about = "拍电影不错")
        val reply = controller.update(user, Const.TEST_TOKEN)
        println(reply.toString())
    }

    @Test
    @Throws(Exception::class)
    fun getUser() {
        val reply = controller.getUser(2, Const.TEST_TOKEN)
        println(reply.toString())
    }

    @Test
    @Throws(Exception::class)
    fun friendRelation() {
        val friend = Friend(fid = 1, uid = 2,relation = 3, fTime = System.currentTimeMillis());
        val reply = controller.friendRelation(friend, Const.TEST_TOKEN)
        println(reply.toString())
    }

    @Test
    @Throws(Exception::class)
    fun getFriendList() {
        val reply = controller.getFriendList(1, 3, Const.TEST_TOKEN)
        println(reply.toString())
    }


}
